/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_MENU__
#define __CC_PYTHON_IMPORT_O_MENU__

#include <cocos2d.h>
#include "python_object_nodeobjects.h"

#define CC_MENUITEM_WRAPPER_DEF(otype)\
	CC_OBJECT_WRAPPER_DEF_BEGIN(otype)\
	CCLAYER_OBJECT_WRAPPER_DEF(otype)\
	void OnClick(CCObject* o);\
	CC_OBJECT_WRAPPER_DEF_END\

#define CC_MENUITEM_WRAPPER_IMPL(otype, ptype)\
	CCNODE_OBJECT_WRAPPER_IMPL(otype, ptype)\
	void __##otype##Wrapper::OnClick(cocos2d::CCObject* o)\
	{\
		if(m_pyObject)\
		{\
			IWrapper *wrap = dynamic_cast<IWrapper *>(o);\
			PyObject *p = Py_None;\
			if(wrap)\
			{\
				p = CPyCCObject::create_object(wrap->GetPythonType(), o);\
			}else\
				p = (PyObject *)CPyCCObject::create<CPyCCObject>(o);\
			ptype *py = (ptype *)m_pyObject;\
			py->OnClick(p);\
		}\
	}\

NS_CCPY_BEGIN

	CC_OBJECT_WRAPPER_DEF_BEGIN(CCMenu)
	CCLAYER_OBJECT_WRAPPER_DEF(CCMenu)
	CC_OBJECT_WRAPPER_DEF_END

	class CPyMenu : public CPyLayerRGBA
	{
	PYCLASS_DEF_INTERFACE(CPyMenu)
	public:
		CPyMenu();		
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsVertically);
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsVerticallyWithPadding);
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsHorizontally);
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsHorizontallyWithPadding);
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsInColumns);
		PYCLASS_METHOD_DEF(CPyMenu, alignItemsInRows);
		PYCLASS_SET_DEF(CPyMenu, HandlerPriority);
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItem)
	class CPyMenuItem : public CPyNodeRGBA
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItem)
	public:
		CPyMenuItem();
		void freeFn()
		{
			Py_SAFE_DECREF(m_fn);
		};
		void OnClick(PyObject *p)
		{
			if(m_fn)
			{
				PyObject *params = PyTuple_Pack(1, p);
				PyObject_Call(m_fn, params, NULL);
				Py_SAFE_DECREF(params);
			}
		}
		void setFn(PyObject *fn, bool rf = true)
		{
			if(rf)
				Py_SAFE_DECREF(m_fn);
			m_fn = fn;
			Py_XINCREF(fn);
		}

		PYCLASS_METHOD_DEF(CPyMenuItem, activate);
		PYCLASS_METHOD_DEF(CPyMenuItem, selected);
		PYCLASS_METHOD_DEF(CPyMenuItem, unselected);
		PYCLASS_GET_DEF(CPyMenuItem, rect);
		PYCLASS_GET_DEF(CPyMenuItem, Selected);
		PYCLASS_SET_DEF(CPyMenuItem, Target);
	private:
		PyObject *m_fn;
	};
	
	CC_MENUITEM_WRAPPER_DEF(CCMenuItemLabel)
	class CPyMenuItemLabel : public CPyMenuItem
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemLabel)
	public:
		CPyMenuItemLabel();
		PYCLASS_METHOD_DEF(CPyMenuItemLabel, setString);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemLabel, DisabledColor);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemLabel, Label);
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItemAtlasFont)
	class CPyMenuItemAtlasFont : public CPyMenuItemLabel
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemAtlasFont)
	public:
		CPyMenuItemAtlasFont();
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItemFont)
	class CPyMenuItemFont : public CPyMenuItemLabel
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemFont)
	public:
		CPyMenuItemFont();
		PYCLASS_METHOD_DEF(CPyMenuItemFont, setDefaultFontSize);
		PYCLASS_METHOD_DEF(CPyMenuItemFont, getDefaultFontSize);
		PYCLASS_METHOD_DEF(CPyMenuItemFont, setDefaultFontName);
		PYCLASS_METHOD_DEF(CPyMenuItemFont, getDefaultFontName);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemFont, FontSize);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemFont, FontName);
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItemSprite)
	class CPyMenuItemSprite : public CPyMenuItem
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemSprite)
	public:
		CPyMenuItemSprite();
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemSprite, NormalImage);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemSprite, SelectedImage);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemSprite, DisabledImage);
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItemImage)
	class CPyMenuItemImage : public CPyMenuItemSprite
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemImage)
	public:
		CPyMenuItemImage();
		PYCLASS_SET_DEF(CPyMenuItemImage, NormalSpriteFrame);
		PYCLASS_SET_DEF(CPyMenuItemImage, SelectedSpriteFrame);
		PYCLASS_SET_DEF(CPyMenuItemImage, DisabledSpriteFrame);
	};

	CC_MENUITEM_WRAPPER_DEF(CCMenuItemToggle)
	class CPyMenuItemToggle : public CPyMenuItem
	{
	PYCLASS_DEF_INTERFACE(CPyMenuItemToggle)
	public:
		CPyMenuItemToggle();		
		PYCLASS_METHOD_DEF(CPyMenuItemToggle, initWithItem);
		PYCLASS_METHOD_DEF(CPyMenuItemToggle, addSubItem);
		PYCLASS_GET_DEF(CPyMenuItemToggle, selectedItem);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemToggle, SelectedIndex);
		PYCLASS_GETSET_MAPPING_DEF(CPyMenuItemToggle, SubItems);
	};
NS_CCPY_END

#endif